﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Reflection;

namespace Baubixinh_client
{
    public partial class frmExportReturn : DevExpress.XtraEditors.XtraForm
    {
        private int mintDonHang;
        private int mintKhoHang;
        private DataTable mobjData;
        public bool DaTraLai { get; set; }
        private bool mblnDangTinhToan;

        public frmExportReturn(DataTable objData, int intDonHang, int intKhoHang)
        {
            InitializeComponent();

            this.mobjData = objData;
            this.mintDonHang = intDonHang;
            this.mintKhoHang = intKhoHang;
        }

        private void frmExportReturn_Load(object sender, EventArgs e)
        {
            try
            {
                if (!mobjData.Columns.Contains("so_luong_tralai"))
                    mobjData.Columns.Add("so_luong_tralai", typeof(int));

                grdControlTraHang.DataSource = mobjData;
                dgvTraHang.BestFitColumns();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            bool blnSuccess = true;

            try
            {
                if (!xCheckValid()) return;

                clsDbAccess.stDonHangChiTiet stTraLai;
                clsDbAccess.stNhapHangChiTiet stNhap;

                stTraLai = new clsDbAccess.stDonHangChiTiet();
                stNhap = new clsDbAccess.stNhapHangChiTiet();

                Program.gobjTrans = Program.gobjConn.BeginTransaction();

                try
                {
                    for (int i = 0; i <= dgvTraHang.RowCount - 1; i++)
                    {
                        if (!blnSuccess) break;

                        stTraLai.intDon_hang_id = mintDonHang;
                        stTraLai.intProduct_id = clsCommon.fncCnvNullToInt(dgvTraHang.GetRowCellValue(i, colProduct_id));
                        stTraLai.strProduct_name = clsCommon.fncCnvNullToString(dgvTraHang.GetRowCellValue(i, colProduct_name2));
                        stTraLai.intMau_id = clsCommon.fncCnvNullToInt(dgvTraHang.GetRowCellValue(i, colMau_id));
                        stTraLai.strMu_name = clsCommon.fncCnvNullToString(dgvTraHang.GetRowCellDisplayText(i, colMau_name2));

                        stTraLai.intSize_id = clsCommon.fncCnvNullToInt(dgvTraHang.GetRowCellValue(i, colSize_id));
                        stTraLai.strSize_name = clsCommon.fncCnvNullToString(dgvTraHang.GetRowCellDisplayText(i, colSize_name2));
                        stTraLai.intSo_luong = clsCommon.fncCnvNullToInt(dgvTraHang.GetRowCellValue(i, colSo_luong_tralai));
                        stTraLai.intDon_gia = clsCommon.fncCnvNullToInt(dgvTraHang.GetRowCellValue(i, colDon_gia2));
                        stTraLai.intGia_goc = clsCommon.fncCnvNullToInt(dgvTraHang.GetRowCellValue(i, colGia_goc));

                        stTraLai.intKhuyen_mai = clsCommon.fncCnvNullToInt(dgvTraHang.GetRowCellValue(i, colKhuyen_mai2));

                        stNhap.intProduct_id = stTraLai.intProduct_id;
                        stNhap.intKho_id = mintKhoHang;
                        stNhap.intMau_id = stTraLai.intMau_id;
                        stNhap.intSize_id = stTraLai.intSize_id;
                        stNhap.intSo_luong = stTraLai.intSo_luong;

                        blnSuccess = blnSuccess && clsDbAccess.fncInsertDonHangTraLai(Program.gobjDb, Program.gobjTrans, stTraLai);
                        blnSuccess = blnSuccess && clsDbAccess.fncUpdateIncreaseProductQuantity(Program.gobjDb, Program.gobjTrans, stTraLai.intProduct_id, stTraLai.intSo_luong);
                        blnSuccess = blnSuccess && clsDbAccess.fncUpdateIncreaseProductQuantity2(Program.gobjDb, Program.gobjTrans, stNhap);

                    }

                    if (blnSuccess) Program.gobjTrans.Commit();
                    else Program.gobjTrans.Rollback();
                }
                catch { Program.gobjTrans.Rollback(); }

                if (blnSuccess)
                {
                    clsCommon.fncMsgInfo("Trả hàng thành công.");
                    this.Close();
                    this.DaTraLai = true;
                }
                else
                {
                    clsCommon.fncMsgError("Thêm đơn hàng thất bại. Hãy kiểm tra lại dữ liệu.");
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }


        private bool xCheckValid()
        {
            try
            {
                int intSoluongCu;
                int intSoluongMoi;

                for (int i = 0; i <= dgvTraHang.RowCount - 1; i++)
                {
                    intSoluongCu = clsCommon.fncCnvNullToInt(dgvTraHang.GetRowCellValue(i, colSo_luong2));
                    intSoluongMoi = clsCommon.fncCnvNullToInt(dgvTraHang.GetRowCellValue(i, colSo_luong_tralai));

                    if (intSoluongMoi < 0)
                    {
                        clsCommon.fncMsgError("Số lượng trả lại phải lớn hơn 0.");
                        return false;
                    }

                    if (intSoluongMoi > intSoluongCu)
                    {
                        clsCommon.fncMsgError("Số lượng trả lại không thể nhiều hơn số lượng bán ra.");
                        return false;
                    }
                }

                return true;
            }
            catch (Exception ex)
            {
                clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message);
                return false;
            }
        }

        private void dgvTraHang_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                //if (mblnDangTinhToan) return;
                //mblnDangTinhToan = true;

                if (e.Column != colSo_luong_tralai) return;

                int intSoluongMoi;
                int intDonGia;
                int intKhuyenMai;
                int intTotal;

                //tinh lai gia
                for (int i = 0; i <= dgvTraHang.RowCount - 1; i++)
                {
                    intSoluongMoi = clsCommon.fncCnvNullToInt(dgvTraHang.GetRowCellValue(i, colSo_luong_tralai));
                    intDonGia = clsCommon.fncCnvNullToInt(dgvTraHang.GetRowCellValue(i, colDon_gia2));
                    intKhuyenMai = clsCommon.fncCnvNullToInt(dgvTraHang.GetRowCellValue(i, colKhuyen_mai2));

                    intTotal = intSoluongMoi * (intDonGia - intKhuyenMai);

                    dgvTraHang.SetRowCellValue(i, colThanh_tien2, intTotal);
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { mblnDangTinhToan = false; }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}